<?php

namespace Rblog\Module\Admin\Model;

class Arts extends \Rexzy\App\Model {
    
    protected $_table = 'arts';
   
    /**
     * 发布新文章
     * 
     * @param type $artMesg 新文章信息
     * @return boolean 发布文章是否成功
     */
    public function addArt( $artMesg ) {
        $result = $this->insert( $artMesg );
        if ( $result > 0 ) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 查询某个文章标题是否已经存在
     * 
     * @param string $title 要查询的文章标题
     * @return array 返回查询结果
     */
    public function existTitle( $title ) {
        $where = array( 'title' => $title );
        $result = $this->select( 'title', $where );
        return count( $result );
    }
    
    /**
     * 查询符合条件的文章
     * 
     * @param array() $condition 查询的排除条件
     * @param int $start 开始查询的位置
     * @param int $length 查询的结果量
     * @return array 查询结果
     */
    public function getList( $condition, $start = 0, $length = 100000 ) {
        $params = array();
        if ( empty( $condition ) ) {
            $where = "group by artid order by artid desc";
            
        } else {
            $wherestr = array();
            if ( isset( $condition['author'] ) ) {
                $wherestr[] = "author = ?";
                $params[] = $condition['author'];
            }
            if ( ( isset( $condition['keyword'] ) ) && ( $condition['keyword'] != '' ) ) {
                $wherestr[] = "title like ?";
                $params[] = "%" . $condition['keyword'] . "%";
            }
            if ( ( isset( $condition['sort'] ) ) && ( $condition['sort'] != 0 ) ) {
                $wherestr[] = "find_in_set(?, sort)";
                $params[] = $condition['sort'];
            }
            
            if ( count( $wherestr ) > 0 ) {
                $where = "where " . implode( 'AND', $wherestr );
                $where .= " group by artid order by artid desc";
            } else {
                $where = "group by artid order by artid desc";
            }
        }
        
        $limit = $start . ', ' . $length; 
        $result = $this->selectAll( '*', $where, $limit, $params );
        return $result;
    }
    
    /**
     *  删除文章
     * @param array $artids 要删除的文章id
     * @return type 删除结果
     */
    public function delArt( $artids ) {
        $wherestr = array();
        $params = array();
        foreach ( $artids as $key => $val ) {
            $wherestr[] = "artid = ?";
            $params[] = $val; 
        }
        $where = implode( ' or ', $wherestr );
        
        $result = $this->delete( $where, $params );
        return $result;
    }
    
    /**
     *  查询某篇文章的详细信息 
     * 
     * @param int $artid 要查询的文章id
     * @return array 查询结果
     */
    public function getMesg( $artid ) {
       return  $this->select( '*', "where artid = $artid"); 
    }
    
    /**
     * 更新编辑文章
     * 
     * @param array $mesg 更新文章数据内容
     * @return int 更新结果
     */
    public function edit( $mesg ) {
        $where = array( 'artid' => $mesg['artid'] );
        $date = array(
            'title' => $mesg['title'],
            'sort' => $mesg['sort'],
            'content' => $mesg['content'],
            'date' => $mesg['date'],
            'outline' => $mesg['outline']
        );
        
        $result = $this->update( $date, $where );
        return $result;
    }
}

